package org.ace.logic.user.impl;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import javax.annotation.Resource;
import static org.ace.logic.user.protocol.PlayerItemProtocol.*;
import io.netty.channel.Channel;

import org.ace.biz.user.IPlayerItemBiz;
import org.ace.dao.model.PlayerItem;
import org.ace.endingCode.SocketModel;
import org.ace.logic.IHandlerServ;
import org.ace.logic.user.AbstractUserHandlerImpl;
import org.ace.logic.user.dto.ItemProcessDTO;
import org.ace.logic.user.dto.PlayerItemDTO;
import org.ace.tool.IExecutorPool;

public class PlayerItemHandlerServImpl extends AbstractUserHandlerImpl
		implements IHandlerServ {

	@Resource(name="executorPool")
	private IExecutorPool executorPool;
	@Resource(name="playerItemBiz")
	private IPlayerItemBiz playerItemBiz;
	
	@Override
	public void Colse(Channel channel) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void process(Channel channel, SocketModel message) {
		switch(message.getCommand())
		{
		case ADD_ITEM_CREQ:
			add(channel,message);
			break;
		case GET_LIST_CREQ:
			getList(channel);
			break;
		case DELETE_ITEM_CREQ:
			delete(channel ,message);
			break;
		case SPEND_ITEM_CREQ:
			spend(channel,message);
			break;
		}

	}

	/*使用道具*/
	private void spend(final Channel channel, SocketModel<Integer> message) {
		final Integer userId = getUserId(channel);
		final Integer gdCode=message.getMessage();
		
		executorPool.execute(new Runnable() {
			
			@Override
			public void run() {
				if(playerItemBiz.spend(userId, gdCode, 1))
				{
					write(channel,SPEND_ITEM_SRES,gdCode);//返回客户端使用的道具gdCode
				}				
			}
		});
	}

	/*d丢弃道具*/
	private void delete(final Channel channel, SocketModel<Integer[]> message) {
		// 客户端传送来的消息 gdCode和 count
		final Integer userId=getUserId(channel);
		final Integer gdCode=message.getMessage()[0];
		final Integer count=message.getMessage()[1];
		
		executorPool.execute(new Runnable() {
			
			@Override
			public void run() {
				//删除道具后 返回 客户端消息
				if(playerItemBiz.delete(userId, gdCode, count));
				write(channel,DELETE_ITEM_SRES,gdCode);
			}
		});
	}

	private void getList(Channel channel) {
		Integer userId=getUserId(channel);
		//取得用户的 itemlist
		Collection<PlayerItem> list=playerItemBiz.getList(userId);
		
		//将 PlayerItem 数据模型列表 转换为 PlayerItemDTO 传输模型列表
		PlayerItemDTO[] dtos=new PlayerItemDTO[list.size()];
		int i = 0;
		for(PlayerItem playerItem : list)
		{
			dtos[i] = modelToDTO(playerItem);
			i++;
		}
		//给客户端发送 物品列表数据
		Arrays.sort(dtos);
		write(channel,GET_LIST_SRES,dtos);
	}

	private PlayerItemDTO modelToDTO(PlayerItem model) {
		PlayerItemDTO dto = new PlayerItemDTO();
		dto.gdCode=model.getGdCode();
		dto.count=model.getCount(); 
		dto.grid=model.getGrid();
		return dto;
	}

	private void add(final Channel channel, SocketModel<ItemProcessDTO> message) {
		final Integer userId= getUserId(channel);
		final ItemProcessDTO dto = message.getMessage();
		final Integer gdCode = dto.gdCode;
		final Integer count = dto.count;
		executorPool.execute(new Runnable() {
			
			@Override
			public void run() {
				if(playerItemBiz.add(userId,gdCode,count))
				{
					write(channel,ADD_ITEM_SRES,dto);
				}
			}
		});
	}

}
